/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.ui.playlist.Item');

goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.ui.Tooltip');


/** @const */ var ITEM_STATUS_NORMAL = 'playlist-item';
/** @const */ var ITEM_STATUS_ACTIVE = 'playlist-item-active';
/** @const */ var ITEM_STATUS_SELECTED = 'playlist-item-selected';

kramerius.audio.ui.playlist.Item = function(track){
    this.track_ = track;
    this.rootElement_ = undefined;
    this.playlist_ = undefined;
}

kramerius.audio.ui.playlist.Item.prototype.appendToPlaylist = function(playlist){
    this.rootElement_ = this.createDom_();
    playlist.getListElement().appendChild(this.rootElement_);
    playlist.addItem(this);
    this.playlist_ = playlist;
}


kramerius.audio.ui.playlist.Item.prototype.createDom_ = function(){
    var thisItem = this;
    
    //root element
    var rootEl = goog.dom.createDom('div',{
        'class':ITEM_STATUS_NORMAL
    });
    
    //button "up in playlist"
    var upEl = goog.dom.createDom('span',{
        'class':'playlist-item-up-enabled'    
    });
    goog.events.listen(upEl, goog.events.EventType.CLICK,
        function (e) {
            thisItem.moveUpInPlaylist();
        });
    goog.events.listen(upEl, goog.events.EventType.DBLCLICK,
        function (e) {
            console.log("nothing");
        });
    rootEl.appendChild(upEl);
    
    
    //button "down in playlist"
    var downEl = goog.dom.createDom('span',{
        'class':'playlist-item-down-enabled'    
    });
    goog.events.listen(downEl, goog.events.EventType.CLICK,
        function (e) {
            thisItem.moveDownInPlaylist();
        });
    rootEl.appendChild(downEl);
    
    
    //button "remove from playlist"    
    var removeEl = goog.dom.createDom('span',{
        'class':'playlist-item-remove'    
    });
    goog.events.listen(removeEl, goog.events.EventType.CLICK,
        function (e) {
            thisItem.removeFromPlaylist();
        });
    rootEl.appendChild(downEl);
    rootEl.appendChild(removeEl);
    
    //TODO: title a description dat do vlastniho spanu
    //ten pak bude odchytavat akce, bude tam naveseny tooltip
    
    //title
    var titleEl = goog.dom.createDom('span',{
        'class':'playlist-item-title unselectable'
        
    });
    titleEl.appendChild(document.createTextNode(this.track_.getTitle()));
    new goog.ui.Tooltip(titleEl, this.track_.getTitle());
    goog.events.listen(titleEl, goog.events.EventType.DBLCLICK,
        function (e) {
            thisItem.playThisItem();
        });
    goog.events.listen(titleEl, goog.events.EventType.CLICK,
        function (e) {
            thisItem.selectThisItem();
        });
    rootEl.appendChild(titleEl);
    
    
    //description
    var descEl = goog.dom.createDom('span',{
        'class':'playlist-item-desc unselectable'    
    });
    descEl.appendChild(document.createTextNode(this.track_.getLength()));   
    new goog.ui.Tooltip(descEl, this.track_.getTitle());
    goog.events.listen(descEl, goog.events.EventType.DBLCLICK,
        function (e) {
            thisItem.playThisItem();
        });
    goog.events.listen(descEl, goog.events.EventType.CLICK,
        function (e) {
            thisItem.selectThisItem();
        });
    rootEl.appendChild(descEl);
    
    return rootEl;
}

kramerius.audio.ui.playlist.Item.prototype.getTrack = function(){
    return this.track_;
}

kramerius.audio.ui.playlist.Item.prototype.moveUpInPlaylist = function(){
    var olderSibling = goog.dom.getPreviousElementSibling(this.rootElement_);
    if (olderSibling!= undefined){
        goog.dom.insertSiblingBefore(this.rootElement_, olderSibling);    
    }
}

kramerius.audio.ui.playlist.Item.prototype.moveDownInPlaylist = function(){
    var youngerSibling = goog.dom.getNextElementSibling(this.rootElement_);
    if (youngerSibling != undefined){
        goog.dom.insertSiblingAfter(this.rootElement_, youngerSibling);    
    }
}

kramerius.audio.ui.playlist.Item.prototype.removeFromPlaylist = function(){
    goog.dom.removeNode(this.rootElement_);
//TODO: pokud hraje, zastavit prehravani
// a aktivovat item pod ním
}


kramerius.audio.ui.playlist.Item.prototype.setRootElementClass = function(elementClass){
    this.rootElement_.setAttribute("class", elementClass);
}

kramerius.audio.ui.playlist.Item.prototype.setStatusNormal = function(){
    this.setRootElementClass(ITEM_STATUS_NORMAL);
}

kramerius.audio.ui.playlist.Item.prototype.setStatusSelected = function(){
    this.setRootElementClass(ITEM_STATUS_SELECTED);
}

kramerius.audio.ui.playlist.Item.prototype.setStatusActive = function(){
    this.setRootElementClass(ITEM_STATUS_ACTIVE);
}

kramerius.audio.ui.playlist.Item.prototype.getStatus = function(){
    var rootElementClass = this.rootElement_.className;
    switch(rootElementClass){
        case ITEM_STATUS_ACTIVE:
            return "ACTIVE";
        case ITEM_STATUS_NORMAL:
            return "NORMAL";
        case ITEM_STATUS_SELECTED:
            return "SELECTED";
        default:
            return undefined;
    }
}

kramerius.audio.ui.playlist.Item.prototype.playThisItem = function(){
    this.playlist_.playItem(this);
}

kramerius.audio.ui.playlist.Item.prototype.selectThisItem = function(){
    this.playlist_.selectItem(this);
}

kramerius.audio.ui.playlist.Item.prototype.statusIsActive = function(){
    return this.getStatus() == "ACTIVE";
}

kramerius.audio.ui.playlist.Item.prototype.statusIsSelected = function(){
    return this.getStatus() == "SELECTED";
}

kramerius.audio.ui.playlist.Item.prototype.statusIsNormal = function(){
    return this.getStatus() == "NORMAL";
}

